package com.kaolafm.kradio.k_navi_map.location.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.amap.api.navi.model.NaviLatLng;
import com.kaolafm.kradio.k_navi_map.R;
import com.kaolafm.kradio.k_navi_map.base.BaseMapFragment;
import com.kaolafm.kradio.k_navi_map.listener.OnMapViewTouchListener;
import com.kaolafm.kradio.k_navi_map.location.OnLocationListener;
import com.kaolafm.kradio.k_navi_map.location.bean.MapLocationData;
import com.kaolafm.kradio.k_navi_map.location.factory.MapViewFactory;
import com.kaolafm.kradio.k_navi_map.location.present.LocationMapPresent;
import com.kaolafm.kradio.k_navi_map.location.view.ILoctionMapView;

/**
 * Created by kaolafm on 2018/4/15.
 */
public class LocationMapFragment extends BaseMapFragment<LocationMapPresent> implements ILoctionMapView{

    private ImageView mLocationImageView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mSavedInstanceState = savedInstanceState;
        mContext = getActivity();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter.startLocation(new OnLocationListener() {
            @Override
            public void locationSuccess(MapLocationData data) {
                mMapViewManager.updateLocationMark(data);
            }

            @Override
            public void locationFailed() {

            }
        });
    }

    @Override
    public void initView(View view) {
        mMapViewManager = MapViewFactory.getInstance().getMapViewManager();
        mMapViewManager.setView(view.findViewById(R.id.map_location), mSavedInstanceState, mContext);
        mMapViewManager.setOnMapViewTouchListener(new OnMapViewTouchListener() {
            @Override
            public void onTouch(MotionEvent event) {
                mMapViewManager.setUseMoveToLocationWithMapMode(false);
            }
        });
        mLocationImageView = view.findViewById(R.id.iv_location);
        mLocationImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mMapViewManager.moveLocationAndMap();
                mMapViewManager.setTrafficEnabled(true);
                mPresenter.calculateSingleDriveRoute(new NaviLatLng(mMapViewManager.getCurLatLng().latitude, mMapViewManager.getCurLatLng().longitude),
                        new NaviLatLng(40.084894,116.603039));
            }
        });
        mPresenter.setMapViewManager(mMapViewManager);
    }

    @Override
    protected int getLayoutId() {
        return MapViewFactory.getInstance().initView("amap");
    }

    @Override
    protected LocationMapPresent createPresenter() {
        return new LocationMapPresent(this);
    }

    @Override
    public Context getActivityContext() {
        return this.getContext();
    }
}
